package com.danciyixue.boot.client.userStudyRecords.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.danciyixue.boot.common.result.PageResult;
import com.danciyixue.boot.common.result.Result;
import com.danciyixue.boot.dcyx.model.form.UserStudyRecordsForm;
import com.danciyixue.boot.dcyx.model.form.UserStudyRecordsUpdateForm;
import com.danciyixue.boot.dcyx.model.query.UserStudyRecordsQuery;
import com.danciyixue.boot.dcyx.model.vo.UserStudyRecordsVO;
import com.danciyixue.boot.dcyx.service.UserStudyRecordsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 客户端用户学习控制器
 */
@Tag(name = "15. 学习记录", description = "15. 学习记录")
@RestController
@RequestMapping("/api/client/v1/user-study-record")

@RequiredArgsConstructor
public class DcyxUserStudyRecordsController {

    private final UserStudyRecordsService userStudyRecordsService;

    /**
     * 获取用户学习记录数据
     */
    @Operation(summary = "获取用户学习记录数据")
    @GetMapping("/page")
    public PageResult<UserStudyRecordsVO> page(UserStudyRecordsQuery queryParams) {
        IPage<UserStudyRecordsVO> page = userStudyRecordsService.getUserStudyRecordsPage(queryParams);
        return PageResult.success(page);
    }

    @Operation(summary = "新增用户学习记录")
    @PostMapping
    public Result<Void> saveUserStudyRecords(@RequestBody @Valid UserStudyRecordsForm formData ) {
        boolean result = userStudyRecordsService.saveUserStudyRecords(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取用户学习记录表单数据")
    @GetMapping("/{id}/form")
    public Result<UserStudyRecordsForm> getUserStudyRecordsForm(
            @Parameter(description = "用户学习记录ID") @PathVariable Long id
    ) {
        UserStudyRecordsForm formData = userStudyRecordsService.getUserStudyRecordsFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改用户学习记录")
    @PutMapping(value = "/{id}")
    public Result<Void> updateUserStudyRecords(
            @Parameter(description = "用户学习记录ID") @PathVariable Long id,
            @RequestBody @Validated UserStudyRecordsForm formData
    ) {
        boolean result = userStudyRecordsService.updateUserStudyRecords(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除用户学习记录")
    @DeleteMapping("/{ids}")
    public Result<Void> deleteUserStudyRecordss(
            @Parameter(description = "用户学习记录ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = userStudyRecordsService.deleteUserStudyRecordss(ids);
        return Result.judge(result);
    }

    @Operation(summary = "新增OR修改用户学习记录")
    @PostMapping("/update")
    public Result<Void> saveOrUpdateUserStudyRecords(@RequestBody @Valid UserStudyRecordsUpdateForm formData) {
        boolean result = userStudyRecordsService.saveOrUpdateUserStudyRecords(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取用户需要复习的单词列表")
    @GetMapping("/review-words")
    public Result<List<com.danciyixue.boot.dcyx.model.vo.WordsVO>> getReviewWords(
            @Parameter(description = "教材ID", required = true) @RequestParam Long programId,
            @Parameter(description = "单元ID，可选") @RequestParam(required = false) Long unitId,
            @Parameter(description = "来源", required = true) @RequestParam String source
    ) {
        List<com.danciyixue.boot.dcyx.model.vo.WordsVO> result = userStudyRecordsService.getReviewWords(programId, unitId, source);
        return Result.success(result);
    }
}